""'''
1.读取excel文件，phone这一列
2.将每一列转成单独的客户链接
3.批量发送
'''
import pandas as pd
import requests

path = input("请输入导入excel的路径： ")
fail_path = "fail.xls"
try:
    df = pd.read_excel(path)
except Exception as e:
    print(e)
    exit()
if 'phone' not in df.columns:
    print("请检查excel文件，没有phone这一列")
    exit()
df['发送结果'] =  ""
column_data = df['phone'].tolist()

for index,phone in enumerate(column_data):
    print("excel中的手机号",phone)
    url = f"http://192.168.1.85:8000/qw/kf/link?phone={phone}"

    res = requests.get(url).json()

    if res["code"] == 200:
        print("客服链接：",res["data"])
        kf_link = res["data"]
        sms_send_link = "http://192.168.1.85:8000/sms/send"
        send_data = {
            "recipient": "86"+str(phone),
            "sender_id": "意数未来",
            # "message": f"合肥独家新产品新政策， 1 对 1 帮您解决资金难题，添加企业微信{kf_link}首次免费咨询，制定个人专属“100% 成功批复金融贷款方案"
            "message": f"123456789dsds"
        }
        res = requests.post(sms_send_link, json=send_data).json()
        print("短信发送结果：",res)
        if res["code"] == 200:
            print("短信发送成功")
            # 将发送结果写入DataFrame的指定列
            df.loc[index, '发送结果'] = '成功'
        else:
            print("短信发送失败")
            df.loc[index, '发送结果'] = '发送短信失败'
    else:
        fail_df = pd.read_excel(fail_path)
        fail_df.loc[len(df)]=[phone]
        fail_df.to_excel(fail_path, index=False)
        print("失败号码",phone)
        df.loc[index, '发送结果'] = '链接生成失败'
# 保存包含发送结果的完整数据到新文件
df.to_excel('发送结果.xlsx', index=False)
